﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Threading;
using System.IO;
using System.IO.Ports;
using Microsoft.Office.Core;
using Excel = Microsoft.Office.Interop.Excel;

namespace TCA_User
{
    public partial class Bill_List
    {
        private void Review_BT_Click(object sender, EventArgs e)
        {
            Load_bill_List();
        }

        private void Add_BT_Click(object sender, EventArgs e)
        {
            if (Check_Bill_Exits_Database(BillNo_txt.Text, Taxi_comboBox.Text) == BILL_EXIST.NEW_BILL)
            {
                Insert_New_Bill();
                Load_bill_List();
            }
        }

        private void Update_BT_Click(object sender, EventArgs e)
        {
            if (Check_Bill_Exits_Database(BillNo_txt.Text, Taxi_comboBox.Text) == BILL_EXIST.BILL_SAME_CARD)
            {
                if ((Get_Bill_Status() == BILL_STATUS.BILL_FREE) ||
                    ((Get_Bill_Status() == BILL_STATUS.BILL_APRROVED) && (User_Info.Permission == ADMIN_LVL) || (User_Info.Permission == MANAGER_LVL)))
                {
                    Update_Bill_Info();
                    Load_bill_List();
                }
            }
            else {
                ///TODO (Kien #1#): Implement if allow for user modify card_ID for bill already Commit ==>DONE
                MessageBox.Show("Not able to change information for this Bill", "Permission Violate");
            }
        }

        private void Approve_Click(object sender, EventArgs e)
        {
            if (Check_Bill_Exits_Database(BillNo_txt.Text, Taxi_comboBox.Text) == BILL_EXIST.BILL_SAME_CARD)
            {
                if ((Get_Bill_Status() == BILL_STATUS.BILL_FREE) || (User_Info.Permission == ADMIN_LVL) || (User_Info.Permission == MANAGER_LVL))
                {
                    Approve_Bill();
                    Load_bill_List();
                }
            }
        }

        private void BusinessAll_BT_Click(object sender, EventArgs e)
        {
            string bill_id, cur_bill_id;
            //Bill_List_dtb
            if (dataGridView_View.SelectedRows.Count > 0)
            {
                foreach (DataGridViewRow row in dataGridView_View.SelectedRows)
                {
                    if ((Get_Cell_String(row.Cells["Approved"]) != "Y") && (Get_Cell_String(row.Cells["Locked"]) != "Y"))
                    {
                        bill_id = row.Cells["Bill_ID"].Value.ToString().Trim();
                        foreach (DataRow tbl_row in Bill_List_dtb.Rows)
                        {
                            cur_bill_id = tbl_row["Bill_ID"].ToString().Trim();
                            if (cur_bill_id == bill_id)
                            {
                                tbl_row["Business"] = "Y";
                                tbl_row["Change_by"] = User_Info.UserName.Trim();
                                tbl_row["Change_DateTime"] = DateTime.Now.ToString("dd MMM yyyy hh:mm:ss");
                                tbl_row["Change_type"] = "Change to Business";
                            }
                        }                        
                    }
                    else
                    {
                        MessageBox.Show("Can not Change Bill Type for Locked Bill: " + Get_Cell_String(row.Cells["Bill_ID"]), "Warning");
                    }
                }
                // dataGridView_View.Update();
                MessageBox.Show("Complete Task", "Success");
            }
            else
            {
                MessageBox.Show("No thing to change", "Warning");
            }
        }

        private void PersonalAll_BT_Click(object sender, EventArgs e)
        {
            string bill_id, cur_bill_id;
            //Bill_List_dtb
            if (dataGridView_View.SelectedRows.Count > 0)
            {
                foreach (DataGridViewRow row in dataGridView_View.SelectedRows)
                {
                    if ((Get_Cell_String(row.Cells["Approved"]) != "Y") && (Get_Cell_String(row.Cells["Locked"]) != "Y"))
                    {
                        bill_id = row.Cells["Bill_ID"].Value.ToString().Trim();
                        foreach (DataRow tbl_row in Bill_List_dtb.Rows)
                        {
                            cur_bill_id = tbl_row["Bill_ID"].ToString().Trim();
                            if (cur_bill_id == bill_id)
                            {
                                tbl_row["Business"] = "N";
                                tbl_row["Change_by"] = User_Info.UserName.Trim();
                                tbl_row["Change_DateTime"] = DateTime.Now.ToString("dd MMM yyyy hh:mm:ss");
                                tbl_row["Change_type"] = "Change to Personal";
                            }
                        }
                    }
                    else
                    {
                        MessageBox.Show("Can not Change Bill Type for Locked Bill: " + Get_Cell_String(row.Cells["Bill_ID"]), "Warning");
                    }
                }
                //dataGridView_View.
                MessageBox.Show("Complete Task", "Success");
            }
            else
            {
                MessageBox.Show("No thing to change", "Warning");
            }
        }

        private void ApproveAll_BT_Click(object sender, EventArgs e)
        {
            string bill_id, cur_bill_id;
            //Bill_List_dtb
            if (dataGridView_View.SelectedRows.Count > 0)
            {
                foreach (DataGridViewRow row in dataGridView_View.SelectedRows)
                {
                    if ((Get_Cell_String(row.Cells["Approved"]) != "Y") && (Get_Cell_String(row.Cells["Locked"]) != "Y"))
                    {
                        bill_id = row.Cells["Bill_ID"].Value.ToString().Trim();
                        foreach (DataRow tbl_row in Bill_List_dtb.Rows)
                        {
                            cur_bill_id = tbl_row["Bill_ID"].ToString().Trim();
                            if (cur_bill_id == bill_id)
                            {
                                tbl_row["Approved"] = "Y";
                                tbl_row["Change_by"] = User_Info.UserName.Trim();
                                tbl_row["Change_DateTime"] = DateTime.Now.ToString();
                                tbl_row["Change_type"] = "Approve";
                            }
                        }
                    }
                    else
                    {
                        MessageBox.Show("Can not Approve for Locked Bill: " + Get_Cell_String(row.Cells["Bill_ID"]), "Warning");
                    }
                }
                MessageBox.Show("Complete Task", "Success");
            }
            else
            {
                MessageBox.Show("No thing to change", "Warning");
            }
        }

        private void LockAll_BT_Click(object sender, EventArgs e)
        {
            string bill_id, cur_bill_id;
            //Bill_List_dtb
            if (dataGridView_View.SelectedRows.Count > 0)
            {
                foreach (DataGridViewRow row in dataGridView_View.SelectedRows)
                {
                    if ((Get_Cell_String(row.Cells["Approved"]) != "Y") && (Get_Cell_String(row.Cells["Locked"]) != "Y"))
                    {
                        bill_id = row.Cells["Bill_ID"].Value.ToString().Trim();
                        foreach (DataRow tbl_row in Bill_List_dtb.Rows)
                        {
                            cur_bill_id = tbl_row["Bill_ID"].ToString().Trim();
                            if (cur_bill_id == bill_id)
                            {
                                tbl_row["Locked"] = "Y";
                                tbl_row["Change_by"] = User_Info.UserName.Trim();
                                tbl_row["Change_DateTime"] = DateTime.Now.ToString();
                                tbl_row["Change_type"] = "Lock";
                            }
                            MessageBox.Show("Complete Task", "Success");
                        }
                    }
                }
            }
            else
            {
                MessageBox.Show("No thing to change", "Warning");
            }
        }

        private void UnLockAll_BT_Click(object sender, EventArgs e)
        {
            string bill_id, cur_bill_id;
            //Bill_List_dtb
            if (dataGridView_View.SelectedRows.Count > 0)
            {
                foreach (DataGridViewRow row in dataGridView_View.SelectedRows)
                {
                    if ((Get_Cell_String(row.Cells["Approved"]) != "Y") && (Get_Cell_String(row.Cells["Locked"]) != "Y"))
                    {
                        bill_id = row.Cells["Bill_ID"].Value.ToString().Trim();
                        foreach (DataRow tbl_row in Bill_List_dtb.Rows)
                        {
                            cur_bill_id = tbl_row["Bill_ID"].ToString().Trim();
                            if (cur_bill_id == bill_id)
                            {
                                tbl_row["Locked"] = "N";
                                tbl_row["Change_by"] = User_Info.UserName.Trim();
                                tbl_row["Change_DateTime"] = DateTime.Now.ToString();
                                tbl_row["Change_type"] = "UnLock";
                            }
                            MessageBox.Show("Complete Task", "Success");
                        }
                    }
                }
            }
            else
            {
                MessageBox.Show("No thing to change", "Warning");
            }
        }

        private void Taxi_comboBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            Load_Card_List_EachProvider();
            Update_BT_Status();
        }

        private void BillNo_txt_TextChanged(object sender, EventArgs e)
        {
            Update_BT_Status();
        }

        private void CardNo_comboBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            Update_BT_Status();
        }

        private void TaxiFee_txt_TextChanged(object sender, EventArgs e)
        {
            Update_BT_Status();
        }

        private void OtherFee_txt_TextChanged(object sender, EventArgs e)
        {
            Update_BT_Status();
        }

        private void Route_txt_TextChanged(object sender, EventArgs e)
        {
            Update_BT_Status();
        }

        private void dataGridView_View_RowEnter(object sender, DataGridViewCellEventArgs e)
        {
            Load_Bill_Info();
        }

        private void dataGridView_View_Mouse_Click(object sender, DataGridViewCellMouseEventArgs e)
        {
            Load_Bill_Info();
        }

        private void Load_Bill_Info()
        {
            if (dataGridView_View.SelectedRows.Count > 0)
            {
                try
                {
                    BillNo_txt.Text = dataGridView_View.SelectedRows[0].Cells["Bill_ID"].Value.ToString();
                    Taxi_comboBox.Text = dataGridView_View.SelectedRows[0].Cells["Taxi_Provider_Name"].Value.ToString();
                    Load_Card_List_EachProvider();
                    Bill_dateTimePicker.Value = (DateTime)dataGridView_View.SelectedRows[0].Cells["Bill_date"].Value;
                    CardNo_comboBox.Text = dataGridView_View.SelectedRows[0].Cells["Card_Provider_Name"].Value.ToString();
                    TaxiID_txt.Text = dataGridView_View.SelectedRows[0].Cells["Taxi_ID"].Value.ToString();
                    Route_txt.Text = dataGridView_View.SelectedRows[0].Cells["Route"].Value.ToString();
                    TaxiFee_txt.Text = dataGridView_View.SelectedRows[0].Cells["Fee"].Value.ToString();
                    OtherFee_txt.Text = dataGridView_View.SelectedRows[0].Cells["Others_Fee"].Value.ToString();
                    Business_check.Checked = dataGridView_View.SelectedRows[0].Cells["Business"].Value.ToString().Trim() == "Y";
                    Note_richTextBox.Text = dataGridView_View.SelectedRows[0].Cells["Bill_Note"].Value.ToString();
                }
                catch
                {
                    BillNo_txt.Text = "";
                    Taxi_comboBox.Text = "";
                    CardNo_comboBox.Text = "";
                    TaxiID_txt.Text = "";
                    Route_txt.Text = "";
                    TaxiFee_txt.Text = "";
                    OtherFee_txt.Text = "";
                    Business_check.Checked = false;
                    Note_richTextBox.Text = "";
                }
            }
        }

        private void ML_Import_Click(object sender, EventArgs e)
        {
            OpenFileDialog open_dialog = new OpenFileDialog();
            string file_name;
            string fInfo;

            open_dialog.Filter = "All files (*.*)|*.*|Excel 97-2003 file (*.xls)|*.xls|Excel file (*.xlsx)|*.xlsx";

            if (open_dialog.ShowDialog() == DialogResult.OK)
            {
                file_name = open_dialog.FileName;
                fInfo = Path.GetExtension(open_dialog.FileName);
            }
            else
            {
                //@Note(Kien): Show test inform can not open file
                MessageBox.Show("Can not open File,");
                return;
            }
            Load_MLExcelfile(file_name);
            Report_Finance();
            
        }

        private void VS_Import_Click(object sender, EventArgs e)
        {
            OpenFileDialog open_dialog = new OpenFileDialog();
            string file_name;
            string fInfo;

            open_dialog.Filter = "All files (*.*)|*.*|Excel 97-2003 file (*.xls)|*.xls|Excel file (*.xlsx)|*.xlsx|Commmand Delimited (*.csv)|*.csv";

            if (open_dialog.ShowDialog() == DialogResult.OK)
            {
                file_name = open_dialog.FileName;
                fInfo = Path.GetExtension(open_dialog.FileName);
            }
            else
            {
                //@Note(Kien): Show test inform can not open file
                MessageBox.Show("Can not open File,");
                return;
            }
            Load_VSExcelfile(file_name);
            Report_Finance();
        }

        private void Bill_List_SizeChanged(object sender, EventArgs e)
        {
            Update_Size(null,null);
        }

        private void Bill_List_SubmitBT_Click(object sender, EventArgs e)
        {
            // dataGridView_View.Rows[0].Cells[0].Selected = false;
            dataGridView_View.Columns[0].Selected = false;
            // dataGridView_Vie
            if (Update_SQL_Data(Bill_List_da, Bill_List_dtb) == true)
            {
                MessageBox.Show("Store Data Complete", "Successful");
            }
            else
            {
                MessageBox.Show("Store Data Fail", "Failed");
            }
        }

        /**************************************************************************************
            #######    #   #        ##           #############       ##  #######   #########
            #      #   #   #        ##                #      ##    ##    #      #  ##       
            #     ##   #   #        ##                #        ## ##     #      #  ##       
            ########   #   #        ##                #         ###      ########  ######## 
            #      ##  #   #        ##                #          #       #         ##       
            #      ##  #   #        ##                #          #       #         ##       
            ########   #   ######## ########          #          #       #         #########
         **************************************************************************************/

        // Show bill list for each card
        private void Bill_Type_SelectedIndexChanged(object sender, EventArgs e)
        {
            Load_bill_List();
        }

        /********************************************************************
          #######      ###      ########   ########         ##  ########  
         ##     ###    # ##     #      ##  ##     ###       ##  ##     ## 
        ##        #   ##  ##    #      ##  ##       #       ##  ##       #
        #            ##   ##    ########   ##       #       ##  ##       #
        ##           ##    ##   #      ##  ##       #       ##  ##       #
         ##     ### ##########  #      ##  ##      ##       ##  ##     ###
          #######   #       ##  #      ##  ########         ##  ########  
         *********************************************************************/

        private void Bill_Tab_AllCardList_SelectedIndexChanged(object sender, EventArgs e)
        {
            Load_bill_List();
        }

        private void BillTab_ShowAllCard_Check_CheckedChanged(object sender, EventArgs e)
        {
            if (BillTab_ShowAllCard_Check.Checked == true)
            {
                Bill_Tab_AllCardList.Enabled = false;
            }
            else
            {
                Bill_Tab_AllCardList.Enabled = true;
            }
            Load_bill_List();
        }

        /**************************************************************************************************
          ######     ###    ########  #######         ######   ##    ##    ## ##     ##  ######## ####### 
        ##     ##   ## #    ##     #  ##     ##      ##     ##  #   ###    #  # ##   ##  #        #      #
        #           ## ##   ## ## ##  ##      #     ##       #  ##  #  #  ##  #  ##  ##  #######  ## #### 
        #          ##   ##  ## #####  ##      #     ##       #   # ##  #  #   #   ## ##  #        ####### 
        ##     ##  #######  ##     #  ##     ##      ##     ##   ###   ####   #    ####  #        ##     #
          ######  #      ## ##     #  #######         ######      ##    ##    #     ###  ######## ##     #
         **************************************************************************************************/

        private void BillTab_CardOwnerList_SelectedIndexChanged(object sender, EventArgs e)
        {
            Load_Card_List();
            Load_bill_List();
        }

        private void BillTab_ShowAllCardOwner_Check_CheckedChanged(object sender, EventArgs e)
        {
            BillTab_CardOwnerList.Enabled = !(BillTab_ShowAllCardOwner_Check.Checked);
            Load_Card_List();
            Load_bill_List();
        }

        // Report to Excel File
        private void Export_BT_Click(object sender, EventArgs e)
        {
            Button bt = (Button) sender;
            Export_Data2Excel(bt, Bill_List_dtb, "Report Bill List");
        }

        private void createFullReportToolStripMenuItem_Click(object sender, EventArgs e)
        {
            
            SaveFileDialog save_diaglog = new SaveFileDialog();
            string file_name, fInfo;

            if (User_Info.Permission != ADMIN_LVL)
            {
                MessageBox.Show("Your Account has no permission for this feature!", "Warning");
                return;
            }

            save_diaglog.Filter = "Excel 98 File (*.xls)|*.xls|Excel File (*.xlsx)|*.xlsx|All files (*.*)|*.*";
            if (save_diaglog.ShowDialog() == DialogResult.OK)
            {
                file_name = save_diaglog.FileName;
                fInfo = Path.GetExtension(save_diaglog.FileName);
                if ((fInfo == ".xls") || (fInfo == ".xlsx"))
                {
                    Create_full_report(file_name);
                }
            }
            MessageBox.Show("Export File thành công", "Thông báo");
        }

        private void Share_DateTime_CheckedChanged(object sender, EventArgs e)
        {
            ML_dateTimePicker_From.Enabled = !(Share_DateTime.Checked);
            ML_dateTimePicker_To.Enabled = !(Share_DateTime.Checked);
        }
    }
}
